﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hiro.ActivationPoints.Interfaces;
using Hiro.Interfaces;
using Hiro.Loaders.Interfaces;

namespace Hiro.Loaders
{
    internal class DependencyMapper : IDependencyMapper
    {
        public IDictionary<IDependency, IList<ITypeActivationPoint>> GetServiceMap(IEnumerable<IDependency> dependencies, IEnumerable<ITypeActivationPoint> activationPoints)
        {
            // Match the dependencies with their corresponding activation points
            var serviceMap = new Dictionary<IDependency,
                IList<ITypeActivationPoint>>();

            foreach (var dependency in dependencies)
            {
                if (!serviceMap.ContainsKey(dependency))
                    serviceMap[dependency] = new List<ITypeActivationPoint>();

                var serviceType = dependency.ServiceType;

                foreach (var point in activationPoints)
                {
                    var implementingType = point.TargetType;

                    if (!serviceType.IsAssignableFrom(implementingType))
                        continue;

                    serviceMap[dependency].Add(point);
                }
            }

            return serviceMap;
        }
    }
}
